#include "Widgets/CheckBoxWidget.h"
#include "Asset/EngineDefaultPackage.h"
RCheckBoxWidget::RCheckBoxWidget()
{
    Mouse=NewWidget<RMouseWidget>(this);
    Mouse->Anchor.Fill(this);

    Mouse->OnClicked.Bind(this,&RCheckBoxWidget::OnClicked);

    SetCheckedImage(REngineDefaultPackage::Get()->GetCheckBoxCheckedIcon());
    SetUncheckedImage(REngineDefaultPackage::Get()->GetCheckBoxUnCheckedIcon());
    SetNotAllCheckedImage(REngineDefaultPackage::Get()->GetCheckBoxNotAllCheckedIcon());
 
}

ECheckBoxWidgetState RCheckBoxWidget::GetCheckState()
{
    return CheckState;
}

void RCheckBoxWidget::SetCheckState(bool InChecked)
{
    SetCheckState(InChecked? ECheckBoxWidgetState::Checked :ECheckBoxWidgetState:: Unchecked);
}

void RCheckBoxWidget::SetCheckState(ECheckBoxWidgetState InNewState)
{
    CheckState=InNewState;

    switch(CheckState)
    {
        case ECheckBoxWidgetState::Checked:
            Image=Checked;
        break;

        case ECheckBoxWidgetState::Unchecked:
            Image=Unchecked;
        break;

        case ECheckBoxWidgetState::Undetermined:
            Image=NotAllChecked;
        break;
    };
}

bool RCheckBoxWidget::OnClicked(const SMouseButtonEvent&)
{
    ECheckBoxWidgetState NewState=
        CheckState==ECheckBoxWidgetState::Checked ?
        ECheckBoxWidgetState::Unchecked :
        ECheckBoxWidgetState::Checked ;

    SetCheckState(NewState);

    OnCheckStateChanged.ExecuteIfBound(NewState);

    return true;
}

void RCheckBoxWidget::SetCheckedImage(RTexture2DPtr InImage)
{
    Checked=InImage;
    if(CheckState==ECheckBoxWidgetState::Checked)
    {
        Image=Checked;
    }
}
void RCheckBoxWidget::SetUncheckedImage(RTexture2DPtr InImage)
{
    Unchecked=InImage;

    if(CheckState==ECheckBoxWidgetState::Unchecked)
    {
        Image=Unchecked;
    }
}
    
void RCheckBoxWidget::SetNotAllCheckedImage(RTexture2DPtr InImage)
{
    NotAllChecked=InImage;

    if(CheckState==ECheckBoxWidgetState::Undetermined)
    {
        Image=NotAllChecked;
    }

}



